﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Xml;

namespace PersonsData
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void exportButton_Click(object sender, EventArgs e)
        {

            firstNameErrorProvider.GetError(firstNameBox)
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.DefaultExt = ".xml";
            sfd.ShowDialog();

            XmlTextWriter writer = new XmlTextWriter(sfd.FileName,Encoding.GetEncoding("utf-8"));
            writer.Indentation = 1;
            writer.IndentChar = '\t';
            writer.Formatting = Formatting.Indented;
            
            using (writer)
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("person");

                writer.WriteStartElement("firstname");
                writer.WriteValue(firstNameBox.Text);
                writer.WriteEndElement();

                writer.WriteStartElement("surname");
                writer.WriteValue(middleNameBox.Text);                
                writer.WriteEndElement();

                writer.WriteStartElement("lastname");
                writer.WriteValue(lastNameBox.Text);
                writer.WriteEndElement();

                writer.WriteStartElement("ssn");
                writer.WriteValue(ssnBox.Text);
                writer.WriteEndElement();

                writer.WriteStartElement("address");
                writer.WriteValue(addressBox.Text);
                writer.WriteEndElement();

                writer.WriteStartElement("phone");
                writer.WriteValue(phoneBox.Text);
                writer.WriteEndElement();

                writer.WriteStartElement("email");
                writer.WriteValue(emailBox.Text);
                writer.WriteEndElement();

                writer.WriteStartElement("website");
                writer.WriteValue(websiteBox.Text);
                writer.WriteEndElement();

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }

        private void nameBox_Validating(object sender, CancelEventArgs e)
        {
            Regex match = new Regex(@"\A[A-Z][a-z]+\Z");
            if (!match.IsMatch((sender as TextBox).Text))
            {
                firstNameErrorProvider.SetError((sender as TextBox), "Name not valid!");
            }
            else
            {
                firstNameErrorProvider.SetError((sender as TextBox), "");
            }
        }

        private void numberBox_Validating(object sender, CancelEventArgs e)
        {
            Regex match = new Regex(@"\A[0-9]+\Z");
            if (!match.IsMatch((sender as TextBox).Text))
            {
                firstNameErrorProvider.SetError((sender as TextBox), "Numeric value expexted!");
            }
            else
            {
                firstNameErrorProvider.SetError((sender as TextBox), "");
            }
        }

        private void emailBox_Validating(object sender, CancelEventArgs e)
        {
            Regex match = new Regex(@"\A[A-Za-z0-9_]+@([A-Za-z0-9_]+\.)+[A-Za-z]{2,4}\Z");
            if (!match.IsMatch((sender as TextBox).Text))
            {
                firstNameErrorProvider.SetError((sender as TextBox), "Email not valid!");
            }
            else
            {
                firstNameErrorProvider.SetError((sender as TextBox), "");
            }
        }

        private void websiteBox_Validating(object sender, CancelEventArgs e)
        {
            Regex match = new Regex(@"\A(http://|www\.)([A-Za-z0-9\-]+\.)+[A-Za-z]{2,4}\Z");
            if (!match.IsMatch((sender as TextBox).Text))
            {
                firstNameErrorProvider.SetError((sender as TextBox), "Email not valid!");
            }
            else
            {
                firstNameErrorProvider.SetError((sender as TextBox), "");
            }
        }
 
    }
}
